package com.zretc.blog.service;

import com.zretc.api.pojo.Blog;
import com.zretc.blog.dao.BlogMapper;
import com.zretc.blog.dao.CommentMapper;
import com.zretc.api.pojo.Comment;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

@Service
public class CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private BlogMapper blogMapper;
    public RespResult addComment(Comment comment){
        if(Objects.isNull(comment.getCommeContent())) {
            return R.fail(WebConstant.BLOG_ERROR_CODE, WebConstant.ERROR_300);
        }
        else{
            commentMapper.insert(comment);
            return R.success("增加成功");
        }

    }

        public HashMap<Object,Object> queryDetailByBlogId(Integer blogId){

        ArrayList<Comment> comment = commentMapper.getCommentByBlogId(blogId);
        Blog blog = blogMapper.getBlogByBlogId(blogId);
            HashMap<Object, Object> resultMap = new HashMap<>();
            resultMap.put("comment",comment);
            resultMap.put("blog",blog);
        return resultMap;
    }

    public RespResult deletecomment(Comment comment){
        if(Objects.isNull(comment.getCommeId())) {
            return R.fail(WebConstant.BLOG_ERROR_CODE, WebConstant.ERROR_404);
        }
        else{
            commentMapper.deletecomment(comment.getCommeId());
            return R.success("删除成功");
        }
    }
    public ArrayList<Comment> queryCommentByUsername(String username){
        ArrayList<Comment> comment = commentMapper.getCommentByUsername(username);

        return comment;
    }

}
